package com.hhkj.admin.bank.common.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hhkj.admin.bank.domain.BankRule;
import com.hhkj.admin.question.common.cont.QuestionTypeEnum;
import com.hhkj.admin.question.domain.Question;
import com.hhkj.admin.question.model.req.QuestionReq;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * BankCreateUtils
 *
 * @author ZhengJun
 * @since 2023/6/17 14:39
 */
public class BankCreateUtils
{
    /**
     * 根据规则获取练习题库题目组列表
     *
     * @param practiceQuestionGroupList 练习题库列表
     * @param allQuestionGroupList      左右题目组列表
     * @return 练习题库题目列表
     */
    public static List<QuestionReq> filterResidueQuestionGroupList(List<QuestionReq> allQuestionGroupList, List<QuestionReq> practiceQuestionGroupList)
    {
        return CollectionUtil.subtractToList(allQuestionGroupList, practiceQuestionGroupList);
    }

    /**
     * 根据规则获取练习题库题目组列表
     *
     * @param rule                 题库规则
     * @param allQuestionGroupList 左右题目组列表
     * @return 练习题库题目列表
     */
    public static List<QuestionReq> filterPracticeQuestionGroupListByRule(BankRule rule, List<QuestionReq> allQuestionGroupList)
    {
        Map<String, List<QuestionReq>> totalQuestionListGroupByTypeCode
                = allQuestionGroupList
                .stream()
                .collect(Collectors.groupingBy(QuestionReq::getTypeCode));

        // 练习题库列表
        List<QuestionReq> practiseQuestionGroupList = new ArrayList<>();
        for (QuestionTypeEnum questionType : QuestionTypeEnum.values()) {
            // 没有对应题目 跳过
            if (!totalQuestionListGroupByTypeCode.containsKey(questionType.getCode())) {
                continue;
            }
            extractQuestionList(
                    totalQuestionListGroupByTypeCode.get(questionType.getCode()),
                    practiseQuestionGroupList,
                    rule.getPracticePercent());
        }
        return practiseQuestionGroupList;
    }

    /**
     * 添加题目信息列表
     *
     * @param origQuestionList   题目来源
     * @param targetQuestionList 目标题目列表
     * @param percent            题目百分比
     */
    public static <T extends Question> void extractQuestionList(List<T> origQuestionList, List<QuestionReq> targetQuestionList, BigDecimal percent)
    {
        if (CollectionUtil.isEmpty(origQuestionList)) {
            return;
        }
        int origQuestionAmount = origQuestionList.size();
        int targetQuestionAmount =
                percent.multiply(BigDecimal.valueOf(origQuestionAmount))
                        .divide(BigDecimal.valueOf(100), 0,
                                RoundingMode.UP
                        ).intValue();

        if (targetQuestionAmount != 0) {
            List<QuestionReq> origQuestionReqList = BeanUtil.copyToList(origQuestionList, QuestionReq.class);
            targetQuestionList.addAll(origQuestionReqList
                    .subList(0, targetQuestionAmount));
        }
    }
}
